package ProjectXXX;

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import module4.*;
import java.util.Vector;

public class AdvancedSettingsPanel extends JPanel implements ActionListener
{
	private JPanel basePanel = null;
	
	private Box checkBoxPanel = null;
	private JLabel choiceLabel = null,priceCBLabel = null,addPriceCBLabel = null,addMHLabel = null;
	private JCheckBox priceCB = null,addPriceCB = null,addMH = null;
	private JButton confirmBut = null;

    private JPanel sectionPanel = null;	
    
    private JPanel pricingPanel = null;
    private JLabel jcbLabel = null,jcbSelectionLabel = null,oldPriceLabel = null;
    private JButton newPriceBut = null;          
    private JTextField newPriceJTF = null;
    private JComboBox catJCB = null;               
    
    private JPanel addPricePanel = null;
    private JLabel catIdLabel = null,catNameLabel = null,catPriceLabel = null;
    private JTextField catJTF1 = null,catJTF2 = null,catJTF3 = null;
    
    private JPanel addMHPanel = null;
    private JLabel siTypeLabel = null,siNameLabel = null,mORhLabel = null,depositLabel = null;
    private JComboBox mhJCB = null;
    private JTextField mhJTF1 = null,mhJTF2 = null,mhJTF3 = null;
    
    //AdvancedSettingsPanelFunctionality
    AdvancedSettingsDAO asf = null;
    private Vector inVect = null;
	
	//fonts
	private Font butFont = null;
	private JFrame testFrame;
	public AdvancedSettingsPanel(Connection inConn)
	{
		//Fonts & Images
		butFont = new Font("ButtonFont",Font.BOLD,15);
		//***********************************************
		
		//Pricing Panel*******************************************
		pricingPanel = new JPanel();
		pricingPanel.setBorder(BorderFactory.createCompoundBorder(
			                   BorderFactory.createEmptyBorder(10,5,5,5),BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.white,Color.white),"PRICING")));
		jcbLabel = new JLabel("SELECT PRICE CATAGROY");	  
		jcbLabel.setBorder(BorderFactory.createEmptyBorder(3,0,40,0));
		//jcbLabel.setMinimumSize(new Dimension(200,10));
		jcbLabel.setMaximumSize(new Dimension(200,10));
		asf = new AdvancedSettingsDAO(inConn);
		jcbSelectionLabel = new JLabel("(SELECTION)");	 
		jcbSelectionLabel.setBorder(BorderFactory.createEmptyBorder(0,0,3,0));     
		newPriceBut = new JButton("SET PRICE");  
		newPriceBut.setBackground(new Color(238,238,238));
		newPriceBut.setBorder(BorderFactory.createRaisedBevelBorder());
		newPriceBut.setFocusPainted(false);
		newPriceBut.addActionListener(this);
		//newPriceBut.setBorderPainted(false);          
		oldPriceLabel = new JLabel("Old Price : ");	
		oldPriceLabel.setBorder(BorderFactory.createEmptyBorder(0,0,3,0));
		newPriceJTF = new JTextField(20);
		newPriceJTF.setEditable(false);
		newPriceJTF.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.black));
		inVect = asf.populatePriceVect(1,jcbSelectionLabel,oldPriceLabel);
		catJCB = new JComboBox(inVect);   
		catJCB.setBorder(BorderFactory.createEmptyBorder(3,0,50,0));
		catJCB.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.black));
		catJCB.addActionListener(this);
		
		GroupLayout layout1 = new GroupLayout(pricingPanel);
		layout1.setAutoCreateContainerGaps(true);
		layout1.setAutoCreateGaps(true);
		pricingPanel.setLayout(layout1);     
		
	    GroupLayout.SequentialGroup hGroup = layout1.createSequentialGroup();
	    hGroup.addGroup(layout1.createParallelGroup().addComponent(jcbLabel).addComponent(jcbSelectionLabel).addComponent(newPriceBut)); 
	    hGroup.addGroup(layout1.createParallelGroup().addComponent(catJCB).addComponent(oldPriceLabel).addComponent(newPriceJTF));  
	    layout1.setHorizontalGroup(hGroup);    	  
	    GroupLayout.SequentialGroup vGroup = layout1.createSequentialGroup();
	    vGroup.addGroup(layout1.createBaselineGroup(true,true).addComponent(jcbLabel).addComponent(catJCB));               
	    vGroup.addGroup(layout1.createBaselineGroup(true,true).addComponent(jcbSelectionLabel).addComponent(oldPriceLabel));               
	    vGroup.addGroup(layout1.createBaselineGroup(true,true).addComponent(newPriceBut).addComponent(newPriceJTF));        
	    layout1.setVerticalGroup(vGroup);     
	    //end pricing panel construction*****************  
			                   
		//addPricePanel  Construction	                   
		addPricePanel = new JPanel();
		catIdLabel = new JLabel("Catagroy Id");
		catIdLabel.setBorder(BorderFactory.createEmptyBorder(15,0,8,0));
		//catIdLabel.setMinimumSize(new Dimension(200,10));
		catIdLabel.setMaximumSize(new Dimension(200,10));
		catNameLabel = new JLabel("Catagory Name");
		catNameLabel.setBorder(BorderFactory.createEmptyBorder(0,0,8,0));
		catPriceLabel = new JLabel("Catagory Price");
		catPriceLabel.setBorder(BorderFactory.createEmptyBorder(0,0,8,0));
		catJTF1 = new JTextField(10);
	    //catJTF1.setMaximumSize(new Dimension(200,20));
		catJTF1.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.black));    
		catJTF2 = new JTextField(10);
		catJTF2.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.black));
		catJTF3 = new JTextField(10);
		catJTF3.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.black));
		addPricePanel.setBorder(BorderFactory.createCompoundBorder(
			                    BorderFactory.createEmptyBorder(5,5,5,5),BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.white,Color.white),"ADD NEW PRICE CATAGORY")));
		GroupLayout layout2 = new GroupLayout(addPricePanel);
		layout2.setAutoCreateContainerGaps(true);
		layout2.setAutoCreateGaps(true);
		addPricePanel.setLayout(layout2);     
		
	    GroupLayout.SequentialGroup hGroup2 = layout2.createSequentialGroup();
	    hGroup2.addGroup(layout2.createParallelGroup().addComponent(catIdLabel).addComponent(catNameLabel).addComponent(catPriceLabel)); 
	    hGroup2.addGroup(layout2.createParallelGroup().addComponent(catJTF1).addComponent(catJTF2).addComponent(catJTF3));  
	    layout2.setHorizontalGroup(hGroup2);    	  
	    GroupLayout.SequentialGroup vGroup2 = layout2.createSequentialGroup();
	    vGroup2.addGroup(layout2.createBaselineGroup(false,true).addComponent(catIdLabel).addComponent(catJTF1));               
	    vGroup2.addGroup(layout2.createBaselineGroup(false,true).addComponent(catNameLabel).addComponent(catJTF2));               
	    vGroup2.addGroup(layout2.createBaselineGroup(false,true).addComponent(catPriceLabel).addComponent(catJTF3));        
	    layout2.setVerticalGroup(vGroup2);    
	    
	    //end addPrice Construction********************             
			                   
		//addMHPanel construction**********************	                   
		addMHPanel = new JPanel();
		addMHPanel.setBorder(BorderFactory.createCompoundBorder(
			                  BorderFactory.createEmptyBorder(5,5,5,5),BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.white,Color.white),"ADD NEW HARDWARE OR MEDIA TYPE")));
		addMHPanel = new JPanel();
		siTypeLabel = new JLabel("Stock Type ID");
		//siTypeLabel.setMinimumSize(new Dimension(200,10));
		siTypeLabel.setMaximumSize(new Dimension(200,10));
		siTypeLabel.setBorder(BorderFactory.createEmptyBorder(15,0,8,0));
		siNameLabel = new JLabel("Stock Type Name");
		siNameLabel.setBorder(BorderFactory.createEmptyBorder(0,0,8,0));
		mORhLabel = new JLabel("Media OR Hardware");
		mORhLabel.setBorder(BorderFactory.createEmptyBorder(0,0,8,0));
		depositLabel = new JLabel("Deposit Value");
		mORhLabel.setBorder(BorderFactory.createEmptyBorder(0,0,8,0));
		mhJTF1 = new JTextField(10);
		mhJTF1.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.black));    
		mhJTF2 = new JTextField(10);
		mhJTF2.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.black));
		mhJTF3 = new JTextField(10);
		mhJTF3.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.black));
		mhJCB = new JComboBox();
		mhJCB.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.black));
		mhJCB.addItem("Media");
		mhJCB.addItem("Hardware");
		
		addMHPanel.setBorder(BorderFactory.createCompoundBorder(
			                    BorderFactory.createEmptyBorder(5,5,5,5),BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.white,Color.white),"ADD NEW MEDIA TYPE OR HARDWARE")));
		GroupLayout layout3 = new GroupLayout(addMHPanel);
		layout3.setAutoCreateContainerGaps(true);
		layout3.setAutoCreateGaps(true);
		addMHPanel.setLayout(layout3);     
		
	    GroupLayout.SequentialGroup hGroup3 = layout3.createSequentialGroup();
	    hGroup3.addGroup(layout3.createParallelGroup().addComponent(siTypeLabel).addComponent(siNameLabel).addComponent(mORhLabel).addComponent(depositLabel)); 
	    hGroup3.addGroup(layout3.createParallelGroup().addComponent(mhJTF1).addComponent(mhJTF2).addComponent(mhJCB).addComponent(mhJTF3));  
	    layout3.setHorizontalGroup(hGroup3);    	  
	    GroupLayout.SequentialGroup vGroup3 = layout3.createSequentialGroup();
	    vGroup3.addGroup(layout3.createBaselineGroup(false,true).addComponent(siTypeLabel).addComponent(mhJTF1));               
	    vGroup3.addGroup(layout3.createBaselineGroup(false,true).addComponent(siNameLabel).addComponent(mhJTF2));               
	    vGroup3.addGroup(layout3.createBaselineGroup(false,true).addComponent(mORhLabel).addComponent(mhJCB));        
	    vGroup3.addGroup(layout3.createBaselineGroup(false,true).addComponent(depositLabel).addComponent(mhJTF3));        
	    layout3.setVerticalGroup(vGroup3);    
		//end MHPanelConstruction
		
		sectionPanel = new JPanel(new GridLayout(0,1));
		sectionPanel.add(pricingPanel);
        sectionPanel.add(addMHPanel);
		sectionPanel.add(addPricePanel);
		choiceLabel = new JLabel("Edited Sections");
		choiceLabel.setFont(butFont);
		choiceLabel.setToolTipText("Please check the boxes below that relate to the sections you will be editing");
		addPriceCB = new JCheckBox();
		addPriceCBLabel = new JLabel("ADD PRICE CATAGORY");
		addMH = new JCheckBox();
		addMHLabel = new JLabel("MEDIA/HARDWARE");
		priceCB = new JCheckBox();
		priceCBLabel = new JLabel("SET PRICE");
		confirmBut = new JButton(new ImageIcon("C:/javausers/IConImages/myButton2.gif"));
	    confirmBut.setBackground(new Color(238,238,238));
		confirmBut.setFocusPainted(false);
		confirmBut.setBorderPainted(false);
		confirmBut.setToolTipText("SELECT TO CONFIRM CHANGES");
		confirmBut.addActionListener(this);
		checkBoxPanel = new Box(BoxLayout.Y_AXIS);
		//confirmBut.setBackground(checkBoxPanel.getBackground());
		checkBoxPanel.add(Box.createVerticalStrut(12));
		checkBoxPanel.add(choiceLabel);
		checkBoxPanel.add(Box.createVerticalStrut(20));
		checkBoxPanel.add(priceCBLabel);
		checkBoxPanel.add(Box.createVerticalStrut(5));
		checkBoxPanel.add(priceCB);
		checkBoxPanel.add(Box.createVerticalStrut(10));
		checkBoxPanel.add(addMHLabel);
		checkBoxPanel.add(Box.createVerticalStrut(5));
		checkBoxPanel.add(addMH);
		checkBoxPanel.add(Box.createVerticalStrut(15));
		checkBoxPanel.add(addPriceCBLabel);
		checkBoxPanel.add(Box.createVerticalStrut(5));
		checkBoxPanel.add(addPriceCB);
		checkBoxPanel.add(Box.createVerticalStrut(228));
		checkBoxPanel.add(confirmBut);
		checkBoxPanel.setBorder(BorderFactory.createCompoundBorder(
			                    BorderFactory.createEmptyBorder(5,5,5,5),BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.white,Color.white),"")));
		
		basePanel = new JPanel(new BorderLayout());
		basePanel.add(checkBoxPanel,BorderLayout.WEST);
		basePanel.add(sectionPanel);
		
		this.setLayout(new GridLayout(1,1));
		this.add(basePanel);
		
		//actionListeners
		catJTF3.addActionListener(this);
		catJTF2.addActionListener(this);
		catJTF1.addActionListener(this);
		mhJTF3.addActionListener(this);
		mhJTF2.addActionListener(this);
		mhJTF1.addActionListener(this);
		newPriceJTF.addActionListener(this);
		
	}//end constructor

    public void actionPerformed(ActionEvent e)
    {
    	Object source = e.getSource();
    	
    	if(source == catJCB)
    	{
    		newPriceJTF.setEditable(false);
    		priceCB.setSelected(false);
    		int selected = (catJCB.getSelectedIndex() + 1); 
    		inVect = asf.populatePriceVect(selected,jcbSelectionLabel,oldPriceLabel);
    	}//end if
    	if(source == newPriceBut)
    	{
    		newPriceJTF.setEditable(true);
    		newPriceJTF.requestFocus();
    		priceCB.setSelected(true);
    	}//end if newPriceBut
    	if(source == confirmBut)
    	{
    		int choice;
    		choice = JOptionPane.showConfirmDialog(null,"-Would you like to Confirm the changes to the following changes?-\n\nPrice set : "+jcbSelectionLabel.getText()+
    		"\n\nNew media OR hardware Type :  "+mhJTF2.getText()+"\n\nNew Price Catagory :  "+catJTF2.getText()+" ","Confirm",JOptionPane.YES_NO_OPTION);
    		
    		if(choice == JOptionPane.YES_OPTION)
    		{
    		
    			if(priceCB.isSelected() == true)
    			{
    				if(newPriceJTF.getText().equals(""))
    				{
    					JOptionPane.showMessageDialog(this,"The new price could not be set for \""+ jcbSelectionLabel.getText()+"\" "+
    					                                   "Please ensure you have entered a new Price");
    				}
    				else
    				asf.setCatPrice(jcbSelectionLabel.getText(),newPriceJTF.getText());
    			}
    			if(addMH.isSelected() == true)
    			{
    				if((mhJTF1.getText().equals("")) || (mhJTF2.getText().equals("")) || (mhJTF3.getText().equals("")))
    				{
    					JOptionPane.showMessageDialog(this,"Catagory \""+mhJTF2.getText()+"\" could not be added.All Fields must be entered");
    				}
    				else
    				{
    					boolean uniqueId;
    					uniqueId = asf.addNewStockType(mhJTF1.getText(),mhJTF2.getText(),mhJCB.getSelectedIndex(),mhJTF3.getText());
    					System.out.println("success??" + uniqueId);
    				}	
    			}
    			if(addPriceCB.isSelected() == true)
    			{
    				if((catJTF1.getText().equals("")) || (catJTF2.getText().equals("")) || (catJTF3.getText().equals("")))
    				{
    					JOptionPane.showMessageDialog(this,"Price Catagory \""+catJTF2.getText()+"\" could not be added.All Fields must be entered");
    				}
    				else
    				{
    					boolean uniqueId;
    					uniqueId = asf.addNewPriceCat(catJTF1.getText(),catJTF2.getText(),Double.parseDouble(catJTF3.getText()));
    					System.out.println("success??" + uniqueId);
    					catJCB.addItem(catJTF2.getText());
    				}	
    			}
    			if((addPriceCB.isSelected() == false) && (priceCB.isSelected() == false) && (addMH.isSelected() == false))
    			{
    				JOptionPane.showMessageDialog(this,"PLEASE SELECT THE CHECK BOXES THAT CORRESPOND TO THE CHANGES YOU HAVE MADE");
    			}
    			clearJTF();
    		}//end if choice = yes_option
    			
    	}//end if confirmBut
    	
    	if((source == mhJTF3) || (source == catJTF3) || (source == newPriceJTF))
    	{
    		confirmBut.requestFocus();
    	}
    	if(source == catJTF1)
    	{
    		catJTF2.requestFocus();
    	}
    	if(source == catJTF2)
    	{
    		catJTF3.requestFocus();
    	}
    	if(source == mhJTF1)
    	{
    		mhJTF2.requestFocus();
    	}
    	if(source == mhJTF2)
    	{
    		mhJCB.requestFocus();
    	}
    	if(source == mhJCB)
    	{
    		mhJTF3.requestFocus();
    	}
    }//end actionPerformed
    
    public void clearJTF()
    {
    	catJTF1.setText("");
    	catJTF2.setText("");
    	catJTF3.setText("");
    	mhJTF1.setText("");
    	mhJTF2.setText("");
    	mhJTF3.setText("");
    	oldPriceLabel.setText(newPriceJTF.getText());
    	newPriceJTF.setText("");
    	newPriceJTF.setEditable(false);
    	priceCB.setSelected(false);
    	addPriceCB.setSelected(false);
    	addMH.setSelected(false);
    }
    
    

}//end class declaration